package com.whynot.frame.basic.mybatis.config;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.whynot.frame.basic.mybatis.config.dynamic.*;
import com.whynot.frame.basic.mybatis.config.properties.CoreDataSource;
import com.whynot.frame.basic.mybatis.config.properties.MutiDataSource;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.whynot.frame.basic.mybatis.scanner.MapperScanner;
import com.zaxxer.hikari.HikariDataSource;
import org.mybatis.spring.annotation.MapperScan;
//import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.*;

/**
 * 描述: MyBatisPlus在SpringBoot下的基础配置:
 * 作者: chenyao
 * 日期: 2018-01-30.
 * 项目名称: curtain-whynot-basic-mybatis
 * 版本: 1.0.0
 * JDK: since 1.8
 */
@Configuration
@EnableTransactionManagement(order = 3,proxyTargetClass = true) //加入事务注解
@MapperScanner(markerInterface = BaseMapper.class,basePackages = "${mybatis-plus.scannerBasePackage:com.whynot.frame.**.mapper}"  )
public class MybatisPlusConfig {


    @Value("${spring.datasource2.url:true}")
    private String url2;

    @Autowired
    CoreDataSource coreDataSource;

    @Autowired
    MutiDataSource mutiDataSource;

    /**
     * 核心数据源
     */
    private HikariDataSource coreDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        coreDataSource.config(dataSource);
        return dataSource;
    }

    /**
     * 另一个数据源
     */
    private HikariDataSource read2DataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        coreDataSource.config(dataSource);
        mutiDataSource.config(dataSource);
        return dataSource;
    }

    //数据源1
    @Bean(name = "datasource")
    @ConditionalOnExpression("'${spring.datasource2.url:true}'=='true'")
    public DataSource dataSource() {
        return coreDataSource();
    }

    /**
     * 动态数据源: 通过AOP在不同数据源之间动态切换
     * @return
     */
    @Bean(name = "dynamicDataSource")
    @ConditionalOnExpression("'${spring.datasource2.url:true}'!='true'")
    public DataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(coreDataSource());
        // 配置多数据源
        Map<Object, Object> dsMap = new HashMap();
        dsMap.put("datasource", coreDataSource());
        dsMap.put("datasource2", read2DataSource());

        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }

    /**
     * mybatis-plus SQL执行效率插件【生产环境可以关闭】
     */
    @Bean
    @Profile({"dev","test"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }

    /*
     * 分页插件，自动识别数据库类型
     * 多租户，请参考官网【插件扩展】
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
